package com.codeavatar.hardwaretester.fragment.hardware;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.codeavatar.catools.base.CaBaseFragment;
import com.codeavatar.catools.helper.CaLogHelper;
import com.codeavatar.catools.helper.CaReceiverHelper;
import com.codeavatar.catools.helper.CaToastHelper;
import com.codeavatar.catools.helper.CaViewHelper;
import com.codeavatar.hardwaretester.R;
import com.codeavatar.hardwaretester.activity.hardware.bluetooth.ServerActivity;
import com.codeavatar.hardwaretester.databinding.FragmentBluetoothCarbleSendcmdBinding;
import com.codeavatar.hardwaretester.model.BluetoothConnectModel;
import com.codeavatar.hardwaretester.service.BluetoothBleService;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class SendCmdFragment extends CaBaseFragment {

    private final String TAG = this.getClass().getSimpleName();

    private FragmentBluetoothCarbleSendcmdBinding mainBind;
    private CaReceiverHelper caReceiverHelper;
    private StringBuffer sbBleMsg;

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private void doInit(){
        sbBleMsg = new StringBuffer("");
        //注册接受者
        caReceiverHelper = new CaReceiverHelper(gActivity,bleChangeBroadcastReceiver);
        caReceiverHelper.registerReceiver(BluetoothBleService.ACTION_APP_BLE_CHANGE, BluetoothBleService.PERMISSION_APP_BLUETOOTH);
    }

    //蓝牙信息通道
    private BroadcastReceiver bleChangeBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            CaLogHelper.info(TAG,"[bleChangeBroadcastReceiver]接收到来自蓝牙服务的消息...");
            Bundle extras = intent.getExtras();
            if(null != extras){
                CaLogHelper.info(extras,"【"+TAG+"】[bleChangeBroadcastReceiver]蓝牙信息通道...");
                String action = extras.getString("action");
                if("ble_response".equals(action)){
                    sbBleMsg.append(extras.getString("value"));
                    sbBleMsg.append("\n");
                    mainBind.ettxMsg.setText(sbBleMsg.toString());
                    mainBind.ettxMsg.scrollTo(0,mainBind.ettxMsg.getScrollY());
                }
            }
        }
    };

    //++++++++++++++++++++++++++++++++++++++
    //++ 重写方法
    //++++++++++++++++++++++++++++++++++++++

    public SendCmdFragment() {
        super(R.layout.fragment_bluetooth_carble_sendcmd);
    }

    @Override
    protected void initBinding(LayoutInflater inflater) {
        mainBind = FragmentBluetoothCarbleSendcmdBinding.inflate(inflater);
    }

    @Override
    protected View getBindRoot() {
        return mainBind.getRoot();
    }

    @Override
    protected void initView() {
        this.doInit();
    }

    @Override
    protected void initEvent() {
        mainBind.btnxSend.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        Bundle extras = new Bundle();
        switch (v.getId()){
            case R.id.btnx_send:
                String cmd = CaViewHelper.init().getViewValue(mainBind.ettxCmd);
                cmd = cmd.replace(" ","");
                extras.clear();
                extras.putString("action","ble_send");
                extras.putString("cmd",cmd);
                caReceiverHelper.sendBroadcast(BluetoothBleService.ACTION_APP_BLE, BluetoothBleService.PERMISSION_APP_BLUETOOTH,extras);
                break;
            default:
                CaLogHelper.warn(TAG, getResources().getString(R.string.ca_txt_no_resid));
                break;
        }
    }

    @Override
    public void onDestroy() {
        caReceiverHelper.unregisterReceiver();
        super.onDestroy();
    }
}
